Page({
  data: {
    longitude: "", //地图界面中心的经度
    latitude: "", //地图界面中心的纬度
    openid:"",
    markers: [ //标志点的位置，这里最后不能写死，可以从数据库中获取数据
      //位置0
      {
        id: 0,
        label:{
          content:"您的位置",
          textAlign:"left",
          color:"#42A5F5"
        },
        iconPath: "../../images/志愿者_男.png",
        latitude:"",
        longitude:"",
        width: 28,
        height: 32
      },
      //位置1
      {
        id: 1,
        title: "老人最爱去的地方",
        iconPath: "../../images/老人.png",
        latitude: 39.8643651273405,
        longitude: 116.37946463513038,
        width: 50,
        height: 50
      },
      //位置2
      {
        id: 2,
        title: "疑似老人走失位置",
        iconPath: "../../images/老人.png",
        latitude: 39.89206499276336,
        longitude: 116.32242997291883,
        width: 50,
        height: 50
      },
      //位置3
      {
        id: 3,
        title: "老人平时散步的地方",
        iconPath: "../../images/老人.png",
        latitude: 39.902910126114556,
        longitude: 116.37974555862456,
        width: 50,
        height: 50
      },
      //位置4
      {
        id: 4,
        title: "疑似老人走失位置",
        iconPath: "../../images/老人.png",
        latitude:39.91626549646871,
        longitude:116.3197564161112,
        width: 50,
        height: 50
      },
      {
        id: 5,
        title: "老人昨天前往此处",
        iconPath: "../../images/老人.png",
        latitude:39.95251786013065,
        longitude:116.41475651597625,
        width: 50,
        height: 50
      },
    ]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    //获取openid
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey
        if(res.code){
          wx.request({
            url: 'https://api.weixin.qq.com/sns/jscode2session',//微信服务器获取appid的网址 不用变
            method:'post',//必须是post方法
            data:{
              js_code:res.code,
              appid:'wx6823eb6b04587eef',//仅为实例appid
              secret:'e2387f22a548a798e6946ac812edff63',//仅为实例secret
              grant_type:'authorization_code'
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            success:function(response){
              console.log(response.data.openid)
              that.setData({
                openid:response.data.openid
              })
              that.getOldmessage();
            }
          })
        }else{
          console.log("登陆失败");
        }

      }
    })
    
    var that = this;
    //拿到AccessToken
    wx.request({
      url: 'http://localhost:8080/getAccessToken',
      success:function(res){
        console.log(res)
      }
    })
    wx.request({
      url: 'url',//获取真实其他志愿者的位置
      
    })
    wx.getLocation({
      type: "gcj02",
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        console.log("当前位置的经纬度为：", res.latitude, res.longitude);
        let temp_lat = 'markers[0].latitude';
        let temp_lon = 'markers[0].longitude';
        that.setData({
          latitude: 39.831215683436,//res.latitude,
          longitude: 116.40820027516645,//res.longitude,
          [temp_lat]: 39.831215683436,//res.latitude,
          [temp_lon]: 116.40820027516645,//res.longitude,
        })
      }
    })
  },
  sendDYMsg: function(e) {
    wx.requestSubscribeMessage({
      tmplIds: ['uoR3mZl7RwsOyx4BRY3g2TOH6HFHETn3FLPm1N5s_8g'],
      success(res) {
        console.log("可以给用户推送信息了");
      }
    })
  },
  onReady:function(){
    
  },
  /**
   * 地图放大缩小事件触发
   * @param {*} e 
   */
  bindregionchange(e) {
    console.log('=bindregiοnchange=', e)
  },
  /**
   * 点击地图事件，有经纬度信息返回
   * @param {*} e 
   */
  bindtapMap(e) {
    console.log('=bindtapMap=', e)
  },

  clickMarker(e){
    this.now_LocationTap(e.detail.markerId);
  },


  getOldmessage(){
    wx.request({
      url: 'http://localhost:8080/sendOpenid_volunteer',//获取真实老人信息的位置
      method:"POST",
      header: { 
        "Content-Type": "application/json" 
        },
      data:{
        openid_volunteer:this.data.openid,
      },
      success:function(res){
        console.log(res.data);
      }
    })
  },
  /**
   * 获取当前位置标记在地图上并且利用polyline绘制路径
   */
  now_LocationTap: function (e) {
        /**********************  从腾讯地图WebService API请求导航路线坐标集合用于point_Array折线连接 */
        var that=this
        var now_Location = String(this.data.markers[0].latitude +','+this.data.markers[0].longitude)//志愿者的位置
        var end_Location = String(this.data.markers[e].latitude +','+this.data.markers[e].longitude)//目的地
        console.log(this.data.markers[0].latitude +','+this.data.markers[0].longitude)
        console.log(this.data.markers[e].latitude +','+this.data.markers[e].longitude)
        wx.request({
          url: 'https://apis.map.qq.com/ws/direction/v1/driving/', //连接接口地址
          data: {
            from: now_Location,
            to: end_Location,
            policy: 'REAL_TRAFFIC',   //结合路况的最优方式
            key: 'KY2BZ-5DD63-HT635-3RAW6-3N6DT-Y4BLJ',
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res.data)
            console.log('剩余距离：' + res.data.result.routes[0].distance + '米')
            console.log('剩余时间：' + res.data.result.routes[0].duration + '分钟')
            console.log('导航路线点串如下：')
            console.log(res.data.result.routes[0].polyline)
            /**  获取返回的方案路线坐标点串并解压 */
            var coors = res.data.result.routes[0].polyline
            for (var i = 2; i < coors.length; i++)
            { coors[i] = coors[i - 2] + coors[i] / 1000000 }
            console.log('路线坐标点串解压完毕!')
            console.log('路线坐标点串解压结果如下：')
            console.log(coors);
            /** 将解压后的坐标点串进行拼接成polyline想要的样子 */
            var coors_new=[]  //记住微信小程序声明一个数组这样写
            for(var j = 0; j < coors.length; j++){
            coors_new.push({
            latitude: parseFloat(coors[j]),
            longitude: parseFloat(coors[j+1])
          })
          j++;
        }
            console.log('路线坐标点串格式化完毕！')
            console.log('路线坐标点串格式化结果如下：')
            console.log(coors)
            console.log('已经产生新的经纬度数组coors_new如下：')
            console.log(coors_new)
            console.log('路线坐标点串解压后一共：' + coors.length + '个')
            console.log('路线坐标点串转换后一共：' + coors_new.length + '个')
            /**  开始同步路线坐标集合+剩余距离+剩余时间数据 */
            that.setData({
              now_Duration: res.data.result.routes[0].duration,   //剩余时间
              now_Distance: res.data.result.routes[0].distance,   //剩余距离
              polyline_Object: [{
                points: coors_new,//指定一系列坐标点，从数组第一项连线至最后一项
                color: "#FF0000DD",
                width: 4,
              }],
            })
            console.log('更新points经纬度数组如下：')
            console.log(that.data.polyline_Object[0].points)
            console.log('更新polyline_Object如下：')
            console.log(that.data.polyline_Object)
            console.log('当前剩余时间 now_Duration同步结果:' + that.data.now_Duration+'分钟')
            console.log('当前剩余距离now_Distance同步结果:' + that.data.now_Distance+'米')
          }
        })
      }
})